package org.boot.controller;

import jakarta.annotation.Resource;
import org.boot.service.AsyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("async")
public class AsyncController {
    private static final Logger logger = LoggerFactory.getLogger(AsyncController.class);

    @Resource
    AsyncService asyncService;

    @RequestMapping("/hi")
    String hi() {
        asyncService.async1();
        CompletableFuture<String> as = asyncService.async2("mcc");
        logger.debug("AsyncController at After");
        as.join();
        if (as.isDone()) {
            try {
                logger.info(as.get());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
        return "Hello AsyncController xxxxwww";
    }

}
